import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.NetPermission;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.management.Query;
import javax.security.auth.kerberos.KerberosKey;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.Session;
import org.junit.Test;

import com.buaa.domain.Institution;
import com.buaa.domain.Instrument;
import com.buaa.domain.Resource;
import com.buaa.util.FilterUtils;
import com.buaa.util.HibernateUtils;
import com.buaa.util.StringUtils;
import com.buaa.util.SymbolUtils;

import javassist.expr.NewArray;

public class test {

	@Test
	public void testXLSX() {
		Set<String> ins = new HashSet<String>();

		try {
			String path = "D:/1.xlsx";
			InputStream ExcelFileToRead = new FileInputStream(path);
			XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
			XSSFSheet sheet = wb.getSheetAt(0);
			XSSFRow row;
			XSSFCell cell = null;
			String name;

			Iterator<Row> rows = sheet.rowIterator();
			while (rows.hasNext()) {
				row = (XSSFRow) rows.next();
				Iterator<Cell> cells = row.cellIterator();
				// while (cells.hasNext()) {
				for (int i = 0; i < 4; i++)
					cell = (XSSFCell) cells.next();
				name = cell.getStringCellValue();
				if (name != null && name != "" && !ins.contains(name))
					ins.add(name);

				// if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
				// System.out.print(cell.getStringCellValue() + " ");
				// } else if (cell.getCellType() ==
				// XSSFCell.CELL_TYPE_NUMERIC) {
				// System.out.print(cell.getNumericCellValue() + " ");
				// } else {
				// // U Can Handel Boolean, Formula, Errors
				// System.out.println("tmp");
				// }
				// }
				// System.out.println();
			}
			wb.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (String string : ins) {
			System.out.println(string);
		}
	}

	@Test
	public void test1() {
		Session session = HibernateUtils.getCurrentSession();
		session.beginTransaction();
		Institution institution = new Institution();
		institution.setCode(1L);
		session.save(institution);
		Instrument instrument = new Instrument();
		session.save(instrument);
		Resource resource = new Resource();
		session.save(resource);
		session.getTransaction().commit();
		// session.close();
	}

	@Test
	public void test2() {
		// String url = "http://ine.has.ac.cn/";
		// url = url.substring(7);

		String url = "ine.h--aS.a111c.Cn";
		url = url.toLowerCase();
		System.out.println(url);

		// String regEx = "^[a-zA-Z0-9//.//-]*\\w+$";
		//
		// // String url = "无";
		// Pattern p = Pattern.compile(regEx);
		// Matcher matcher = p.matcher(url);
		//
		// System.out.println(matcher.matches());

		// url =
		// url.substring(0,url.indexOf('/')==-1?url.length():url.indexOf('/'));
		// System.out.println(url);

	}

	@Test
	public void test3() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		try {
			// Date date = sdf.parse("1000-01-01");
			// System.out.println(date);
			Date date = sdf.parse("2014 11 27 4 5 3");
			System.out.println(date);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
	}

	@Test
	public void test4() {
		String name = "司伟宏";
		System.out.println(isAllChinese(name));

	}

	public boolean isAllChinese(String str) {

		for (int i = 0; i < str.length(); i++) {
			if (!isChinese(str.charAt(i)))
				return false;
		}
		return true;
	}

	public boolean isChinese(char c) {
		return c >= 0x4E00 && c <= 0x9FA5;
	}

	@Test
	public void test5() {
		// String phone ="3333-3333333-";
		String phone = "18810615637";
		String regEx = "^(\\d{4}-\\d{7,8}(-(\\d{2,4})?)?|\\d{11})$";
		Pattern p = Pattern.compile(regEx);
		Matcher matcher = p.matcher(phone);
		System.out.println(matcher.matches());
		// System.out.println(phone.substring(0,phone.length()-1));
	}

	@Test
	public void test6() {
		String fax = "045186604499";
		System.out.println(fax.charAt(4) != '-' ? fax.substring(0, 4) + "/" + fax.substring(4) : fax);
		String regEx = "^\\d{4}(-)?\\d{7,8}$";
		Pattern p = Pattern.compile(regEx);
		Matcher matcher = p.matcher(fax);
		System.out.println(matcher.matches());
	}

	@Test
	public void test7() {
		String base_name = "其他@国家质检总局动物遗传物质检测重点实验室;期@的";
		String regEx = "^[\u4E00-\u9FA5]+@[\u4E00-\u9FA5]+(;[\u4E00-\u9FA5]+@[\u4E00-\u9FA5]+)*$";
		Pattern p = Pattern.compile(regEx);
		Matcher matcher = p.matcher(base_name);
		System.out.println(matcher.matches());
	}

	@Test
	public void test8() {
		String name = "哈尔滨工业大学航天学院惯导测试设备研究中心　";

		System.out.println(getutf8FromString(name));
	}

	public String getutf8FromString(String str) {
		StringBuffer utfcode = new StringBuffer();
		try {
			for (byte bit : str.getBytes("unicode")) {
				char hex = (char) (bit & 0xFF);
				utfcode.append(Integer.toHexString(hex) + " ");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return utfcode.toString();
	}

	@Test
	public void test9() {
		String contact_phone = "0451-86282105转8006/13904619674";
		contact_phone = contact_phone.replace("转", "-").replace("/", "，").replace("、", "，");
		System.out.println(contact_phone);
	}

	@Test
	public void test10() {
		String fax = "0451—84615315";
		String regEx = "^(\\d{4}(-)?)?\\d{7,8}(-\\d{3,4})?$";
		Pattern p = Pattern.compile(regEx);
		Matcher matcher = p.matcher(fax);
		System.out.println(matcher.matches());
	}

	@Test
	public void test12() {
		String str = "0451-55153030/55153678";
//		str = str.replace("—", "-").replace("－", "-").replace("–", "-").replace("	", " ").replace("○", "〇")
//				.replace("′", "’");
//		Set<Character> symbol = new HashSet<Character>();
//		SymbolUtils.addAllSym(symbol);
//		System.out.println(FilterUtils.characterFilter(str, symbol, "", -1));
		System.out.println(FilterUtils.faxFilter(str));
	}

	@Test
	public void getNum() {
		// 中文和全角符号之间的关系究竟是怎样？
		System.out.println((int) ('Г'));
		System.out.println((int) ('O'));
	}

	@Test
	/**
	 * 符号相等测试
	 */
	public void test11_symeql() {

		// System.out.println("－".equals("―"));
		// System.out.println("-".equals("―"));
		System.out.println("：".equals("："));

		System.out.println("；".equals("；"));
		System.out.println("，".equals("，"));
		System.out.println("。".equals("．"));
		System.out.println('。');
		System.out.println('。' - 65248);
	}

	@Test
	/**
	 * 半角全角转换测试
	 */
	public void halfToFull() {
		Set<Character> symbol = new HashSet<Character>();
		// SymbolUtils.addEnSym(symbol);
		// SymbolUtils.addZhSym(symbol);
		SymbolUtils.addGreekSym(symbol);

		// System.out.println(symbol.contains('Ω'));

		// for (Character ch : symbol) {
		// System.out.print(ch + " : ");
		// if (ch == ' ') {
		// ch = '\u3000';
		// } else if (ch < '\177') {
		// ch = (char) (ch + 65248);
		// }
		// System.out.println(ch);
		// }
		//

		// for (int i = 65281; i <= 65374; i++)
		// System.out.print((char)( i-65248)+" ");

		// for (int i = 945; i <= 969; i++) {
		// if (i != 'ς')
		// System.out.print((char) i+" ");
		// }
		// System.out.println();
		// // 'Α'-913 'Ω'-937 '΢'-930
		// for (int i = 'Α'; i <= 'Ω'; i++) {
		// if (i != '΢')
		// System.out.print((char) i+" ");
		//
		// }

		// System.out.println((int)'①');
		for (int i = 33 + 65248; i < 127 + 65248; i++)
			System.out.print((char) i);

		// for (int i = 1279; i >= 1024; i--)
		// System.out.println((char) i);

		// char ch = '、';
		// if (65281 <= ch && ch <= 65374)
		// System.out.println((char) (ch - 65248));
		// System.out.println((int)ch);

		// System.out.println((char) 127);
	}

	@Test
	/**
	 * hashSet是否可以存""
	 */
	public void test15() {
		Set<String> set = new HashSet<String>();
		set.add("");
		System.out.println(set.size());
		for (String string : set) {
			System.out.println("S" + string + "E");
		}
	}

	@Test
	/**
	 * 测试图片url
	 */
	public void test16() {
		String pic = "/upload/cedb3080746f420e83ad1b9741aaa1c5clip_image002.jpg";
		String[] suffixes = { ".jpg", ".jpeg", ".JPEG", ".gif", ".GIF", ".png" };
		System.out.println(FilterUtils.fileFilter(pic, suffixes));
	}

	/**
	 * 空StringBuffer与""相同
	 */
	@Test
	public void test17() {
		System.out.println(new StringBuffer().toString().equals(""));

	}

	@Test
	public void test18() {
		String test1 = "！＂＃＄％＆＇（）＊＋，－．／";
		String test2 = "!\"#$%&'()*+,-./";
		String test3 = "！“#￥%&‘（）*+，-。、";

		char t1;
		char t2;
		char t3;
		for (int i = 0; i < test1.length(); i++) {
			t1 = test1.charAt(i);
			t2 = test2.charAt(i);
			t3 = test3.charAt(i);

			System.out.print(i + ":" + (t1 == t3) + (t2 == t3));
			System.out.println();
		}
	}

	@Test
	public void test19() {
		String str="";
		  long starTime=System.currentTimeMillis();
		  //计算循环10000的时间
		  for(int i=0;i<10000;i++){
		   str=str+i;
		  }
		  long endTime=System.currentTimeMillis();
		  long Time=endTime-starTime;
		  System.out.println(Time);
		
	}
}
